package com.example.demo.controller;


import com.example.demo.pojo.Jilue;
import com.example.demo.service.NumberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;

@RestController
@RequestMapping("api/number")
public class NumberController {
    @Autowired
    private NumberService numberService;

    @GetMapping("/allNumber")
    public ResponseEntity<?> allNumber() {
        int allNumber = numberService.getAllNumbers().size();
        int students = numberService.getAllRole().size();
        int teachers  = allNumber - students;
//        int offstatus = ;
        int status = numberService.getAllStatus().size();

        int teachStatus = numberService.getTeacherStatus().size();

        Map<String, Integer> response = new HashMap<>();
        response.put("allNumber", allNumber);
        response.put("students", students);
        response.put("teachers", teachers);
        response.put("offstatus", teachStatus);
        response.put("status", status);

        return ResponseEntity.ok(response);

    }

    @GetMapping("/warn")
    public ResponseEntity<?> warn() {
        int warnNumber = numberService.getWarn().size();
        int seriousWarnNumber = numberService.getSeriouWarn().size();
        int jiGuoWarnNumber = numberService.getJiGuoWarn().size();
        int seekWarnNumber = numberService.getSeekWarn().size();
        int outWarnNumber = numberService.getOutWarn().size();
        int all_warn = warnNumber + seriousWarnNumber + jiGuoWarnNumber + seekWarnNumber + outWarnNumber;
        if (all_warn == 0) {
            all_warn = 1;
        }
        // 使用浮点数计算并保留1位小数
        float serious_warn_rate = (float) seriousWarnNumber / all_warn * 100;
//        serious_warn_rate = Math.round(serious_warn_rate * 10) / 10.0f;  // 保留1位小数

        // 示例输出
        System.out.println("Serious Warn Rate: " + serious_warn_rate + "%");

        // 其他的变量计算
//        int jiGuoWarnNumber = 50;
        float jiGuo_warn_rate = (float) jiGuoWarnNumber / all_warn * 100;
//        jiGuo_warn_rate = Math.round(jiGuo_warn_rate * 10) / 10.0f;  // 保留1位小数
        System.out.println("JiGuo Warn Rate: " + jiGuo_warn_rate + "%");

//        int seekWarnNumber = 30;
        float seek_warn_rate = (float) seekWarnNumber / all_warn * 100;
//        seek_warn_rate = Math.round(seek_warn_rate * 10) / 10.0f;  // 保留1位小数
        System.out.println("Seek Warn Rate: " + seek_warn_rate + "%");

//        int outWarnNumber = 40;
        float out_warn_rate = (float) outWarnNumber / all_warn * 100;
//        out_warn_rate = Math.round(out_warn_rate * 10) / 10.0f;  // 保留1位小数
        System.out.println("Out Warn Rate: " + out_warn_rate + "%");

//        int warnNumber = 80;
        float warn_rate = (float) warnNumber / all_warn * 100;
//        warn_rate = Math.round(warn_rate * 10) / 10.0f;  // 保留1位小数
        System.out.println("Warn Rate: " + warn_rate + "%");


        Map<String, Integer> response = new HashMap<>();
        response.put("warn", warnNumber);
        response.put("serious_warn_rate", (int) serious_warn_rate);
        response.put("serious_warn", seriousWarnNumber);
        response.put("warn_rate", (int) warn_rate);
        response.put("jiGuo_warn", jiGuoWarnNumber);
        response.put("jiGuo_warn_rate", (int) jiGuo_warn_rate);
        response.put("seek_warn", seekWarnNumber);
        response.put("seek_warn_rate", (int) seek_warn_rate);
        response.put("out_warn", outWarnNumber);
        response.put("out_warn_rate", (int) out_warn_rate);


        return ResponseEntity.ok(response);
    }

    @GetMapping("/dataYear")
    public ResponseEntity<?> dataYear() {
        LocalDate now = LocalDate.now(); // 获取当前日期
        int nowYear = now.getYear(); // 获取当前年份

        System.out.println("Current Year: " + nowYear);
//        System.out.println("Current Month: " + currentMonth);
        String[] numberWords = {
            "one", "two", "three", "four", "five",
            "six", "seven", "eight", "nine", "ten",
            "eleven", "twelve"
        };
        int[][] array = new int[3][12];  // 默认所有元素初始化为 0
        for (int[] ints : array) {
            // 明确赋值为 0
            Arrays.fill(ints, 0);
        }

        List<Jilue> time_list = numberService.getUpdateTime();
        for (Jilue item : time_list) {
            Date createdAt = item.getCreated_at(); // 获取日期
            if (createdAt != null) {
                // 转换 Date 对象为 LocalDateTime
                LocalDateTime createdAtDateTime = createdAt.toInstant()
                                                          .atZone(ZoneId.systemDefault())
                                                          .toLocalDateTime();

                int year = createdAtDateTime.getYear(); // 提取年份
                int month = createdAtDateTime.getMonthValue(); // 提取月份

                System.out.println("Created at: " + createdAtDateTime); // 输出完整时间
                System.out.println("Year: " + year); // 输出年份
                System.out.println("Month: " + month); // 输出月份
                if (year<=nowYear && year >= nowYear -2) {
                    System.out.println(year-nowYear+2);
                    array[year-nowYear+2][month-1] += 1;
                }



            }
        }

         Map<String, List<Object>> response = new HashMap<>();
//
        for (int i = 0; i < 12; i++) {
            List<Object> monthData = new ArrayList<>();
            monthData.add(i+1 + "月");  // 添加月份的中文名称
            monthData.add(array[0][i]);  // 第一行的数据
            monthData.add(array[1][i]);  // 第二行的数据
            monthData.add(array[2][i]);  // 第三行的数据
            response.put(numberWords[i], monthData);
        }
        response.put("nowYear", Collections.singletonList(nowYear));


        return ResponseEntity.ok(response);
    }


}
